<?php 
// EventLoop 命令行聊天服务端

class Server
{
	private $serv;
	private $test;

	public function __construct(){
		$this->serv = new swoole_server("0.0.0.0",9501);
		$this->serv->set(array(
			'worker_num' => 1,
		));
		$this->serv->on('Start',array($this,'onStart'));
		$this->serv->on('Connect',array($this,'onConnect'));
		$this->serv->on('Receive',array($this,'onReceive'));
		$this->serv->on('Close',array($this,'onClose'));

		$this->serv->start();
	}

	public function onStart($serv){
		echo "Start\n";
	}

	public function onConnect($serv,$fd,$from_id){
		echo "Client {$fd} connect\n";
	}

	public function onClose($serv,$fd,$from_id){
		echo "Client {$fd} close connection\n";
	}

	public function onReceive(swoole_server $serv,$fd,$from_id,$data){
		echo "Get Message From Client {$fd}:{$data}\n";
		foreach ($serv->connections as $client) {
			if($fd != $client)
			$serv->send($client,$data);
		}
	}

}

$server = new Server();
